//
//  UIView+BezierTriangle.m
//  DDTL
//
//  Created by lx on 2017/8/3.
//  Copyright © 2017年 lx. All rights reserved.
//

#import "UIView+BezierTriangle.h"

@implementation UIView (BezierTriangle)

/// 绘制曲线
-(void)drawBottomCurveForFocusItems:(CGFloat)h{
    UIColor *color = [UIColor clearColor];
    [color set]; //设置线条颜色
    UIBezierPath* aPath = [UIBezierPath bezierPath];
    
    aPath.lineWidth = 5.0;
    aPath.lineCapStyle = kCGLineCapRound; //线条拐角
    aPath.lineJoinStyle = kCGLineCapRound; //终点处理
    
    [aPath moveToPoint:CGPointMake(0, self.height - h)];
    [aPath addCurveToPoint:CGPointMake(self.width,self.height - h) controlPoint1:CGPointMake(self.width/2 - h, self.height)  controlPoint2:CGPointMake(self.width/2 + h, self.height)];
    [aPath addLineToPoint:CGPointMake(self.width,0)];
    [aPath addLineToPoint:CGPointMake(0, 0)];
    [aPath addLineToPoint:CGPointMake(0, self.height)];

    [aPath stroke];
    [aPath closePath];
    // step 2: 根据路径生成蒙板
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //    _maskLayer.position = self.center;
    maskLayer.path = [aPath CGPath];
    // step 3: 添加蒙版
    self.layer.mask = maskLayer;
    
    //设置背景颜色
    self.backgroundColor = kWhiteColor;
}
/**
 绘制三角形（顶部两边为曲线）

 @param lineColor 边线颜色
 @param bgColor 背景色
 @param point1 左边控制点
 @param point2 右边控制点
 */
-(void)drawTriangleByTopCurveWithLineColor:(UIColor *)lineColor bgColor:(UIColor *)bgColor bendingPoint1:(CGPoint)point1 bendingPoint2:(CGPoint)point2{
    UIColor *color;
    if (lineColor) {
        color = lineColor;
    }else{
        color = [UIColor clearColor];
    }
    [color set]; //设置线条颜色
    
    UIBezierPath* aPath = [UIBezierPath bezierPath];
    
    aPath.lineWidth = 5.0;
    aPath.lineCapStyle = kCGLineCapRound; //线条拐角
    aPath.lineJoinStyle = kCGLineCapRound; //终点处理
    
    [aPath moveToPoint:CGPointMake(0, self.height)];
    [aPath addCurveToPoint:CGPointMake(self.width,self.height) controlPoint1:point1 controlPoint2:point2];
    [aPath addLineToPoint:CGPointMake(0, self.height)];
    [aPath stroke];
    [aPath closePath];
    // step 2: 根据路径生成蒙板
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //    _maskLayer.position = self.center;
    maskLayer.path = [aPath CGPath];
    // step 3: 添加蒙版
    self.layer.mask = maskLayer;
    
    //设置背景颜色
    if (bgColor) {
        self.backgroundColor = bgColor;
    }else{
        self.backgroundColor = kBgColor;
    }
}

/**
 绘制底部为曲线的四边形
 @param lineColor 线条颜色
 @param bgColor 背景颜色
 @param point1 左边控制点
 @param point2 右边控制点
 */
-(void)drawQuadrilateralByTheBottomCurveLineColor:(UIColor *)lineColor bgColor:(UIColor *)bgColor bendingPoint1:(CGPoint)point1 bendingPoint2:(CGPoint)point2{
    UIColor *color = lineColor;
    [color set]; //设置线条颜色
    
    UIBezierPath* aPath = [UIBezierPath bezierPath];
    
    aPath.lineWidth = 5.0;
    aPath.lineCapStyle = kCGLineCapRound; //线条拐角
    aPath.lineJoinStyle = kCGLineCapRound; //终点处理
    
    [aPath moveToPoint:CGPointMake(0, self.height)];
    [aPath addCurveToPoint:CGPointMake(self.width,self.height) controlPoint1:point1 controlPoint2:point2];
    [aPath addLineToPoint:CGPointMake(self.width,0)];
    [aPath addLineToPoint:CGPointMake(0, 0)];
    [aPath addLineToPoint:CGPointMake(0, self.height)];
    [aPath stroke];
    [aPath closePath];
    // step 2: 根据路径生成蒙板
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //    _maskLayer.position = self.center;
    maskLayer.path = [aPath CGPath];
    // step 3: 添加蒙版
    self.layer.mask = maskLayer;
    self.backgroundColor = bgColor;
}

@end
